import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  // return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
  return true;
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('token')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}

export function getDateYYYYMMddHHMMSS(Date){
  const date = Date;
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const strDate = date.getDate().toString().padStart(2, '0');
  const starHours = date.getHours().toString().padStart(2, '0');
  const starMinutes = date.getMinutes().toString().padStart(2, '0');
  const starSeconds = date.getSeconds().toString().padStart(2, '0');
  return `${date.getFullYear()}-${month}-${strDate} ${starHours}:${starMinutes}:${starSeconds}`;
}


export function timeSet()
{
  var date = new Date(); //new Date(value*1000);根据时间戳格式进行选择乘1000或否
  var timeDate;
  Y = date.getFullYear() + '-';
  M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  h = 23 + ':';
  m = 59 + ':';
  s = 59;
  timeDate = Y + M + D + h + m + s;
  return new Date(timeDate).getTime()

}

export function isLength (s) {
  return /^[a-zA-Z0-9_]{1,18}$/.test(s)
}

export function pickerOptions(){
  const pickerOptions = {
    shortcuts: [{
      text: '最近一周',
      onClick(picker) {
        const start = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000);
        const end = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
        picker.$emit('pick', [start, end]);
      }
    }, {
      text: '最近一个月',
      onClick(picker) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        picker.$emit('pick', [start, end]);
      }
    }, {
      text: '最近三个月',
      onClick(picker) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
        picker.$emit('pick', [start, end]);
      }
    }]
  }
  return pickerOptions
}


export function initData()
{
  const initData = [
    {"columnType":"CLOB",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"NVARCHAR2",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"numeric",
      "attrType":"BigDecimal",
      "packageName":"java.math.BigDecimal"
    },
    {"columnType":"int8",
      "attrType":"Long",
      "packageName":""
    },
    {"columnType":"int4",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"int2",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"tinytext",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"text",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"integer",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"varchar",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"char",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"bit",
      "attrType":"Boolean",
      "packageName":""
    },
    {"columnType":"decimal",
      "attrType":"BigDecimal",
      "packageName":"java.math.BigDecimal"
    },
    {"columnType":"double",
      "attrType":"Double",
      "packageName":""
    },
    {"columnType":"float",
      "attrType":"Float",
      "packageName":""
    },
    {"columnType":"datetime",
      "attrType":"Date",
      "packageName":""
    },
    {"columnType":"mediumint",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"smallint",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"tinyint",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"date",
      "attrType":"Date",
      "packageName":"java.util.Date"
    },
    {"columnType":"mediumtext",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"longtext",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"timestamp",
      "attrType":"Date",
      "packageName":""
    },
    {"columnType":"NUMBER",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"INT",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"INTEGER",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"BINARY_INTEGER",
      "attrType":"Integer",
      "packageName":""
    },
    {"columnType":"LONG",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"FLOAT",
      "attrType":"Float",
      "packageName":""
    },
    {"columnType":"BINARY_FLOAT",
      "attrType":"Float",
      "packageName":""
    },
    {"columnType":"DOUBLE",
      "attrType":"Double",
      "packageName":""
    },
    {"columnType":"BINARY_DOUBLE",
      "attrType":"Double",
      "packageName":""
    },
    {"columnType":"DECIMAL",
      "attrType":"BigDecimal",
      "packageName":""
    },
    {"columnType":"CHAR",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"VARCHAR",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"VARCHAR2",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"NVARCHAR",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"BLOB",
      "attrType":"String",
      "packageName":""
    },
    {"columnType":"DATE",
      "attrType":"Date",
      "packageName":"java.util.Date"
    },
    {"columnType":"DATETIME",
      "attrType":"Date",
      "packageName":"java.util.Date"
    },
    {"columnType":"TIMESTAMP",
      "attrType":"Date",
      "packageName":"java.util.Date"
    },
    {"columnType":"TIMESTAMP(6)",
      "attrType":"Date",
      "packageName":"java.util.Date"
    },
    {"columnType":"bigint",
      "attrType":"Long",
      "packageName":""
    },
    {"columnType":"int",
      "attrType":"Integer",
      "packageName":""
    }

  ]
  return initData

}
